package com.aifast.common.base;

import cn.hutool.core.date.DateUtil;
import com.aifast.common.global.Status;
import lombok.Data;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * 接口统一返回格式
 * @author 符健
 */
@Data
public class R implements Serializable {

    private int code ;

    private String msg ;

    private Object data;

    private String currentTime = DateUtil.format(LocalDateTime.now(),"yyyy-MM-dd HH:mm:ss");

    public R() {
        this.code = Status.SUCCESS.getCode();
        this.msg = Status.SUCCESS.getName();
    }

    public R(Status status){
        this.code = status.getCode();
        this.msg = status.getName();
    }

    public R(Object obj){
        this.data = obj;
        this.code = Status.SUCCESS.getCode();
        this.msg = Status.SUCCESS.getName();
    }

    public static R OK(){
        return new R();
    }

    public static R OK(Object obj){
        return new R(obj);
    }

    public static R ERROR(){
        return new R(Status.ERROR);
    }

    public static R ERROR(Status status){
        return new R(status);
    }

    public static R ERROR(String msg){
        R r = new R();
        r.msg = msg;
        r.code = Status.ERROR.getCode();
        return r;
    }


}
